package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.RegionQuery;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * Created by 甘静斌 on 2020-08-11 19:49
 */
public class PageHelperUtil {

    public static <T> Page<T> query(MongoTemplate template, Pageable pageable,
                                    Class<T> clz, Query query, QueryObject qo) {

        //分页需要的数据: total -- 所有数据
        long total = template.count(query, clz);
        if (total == 0){
            return Page.empty();
        }
        query.with(pageable);
        //当前页显示的数据
        List<T> list = template.find(query,clz);

        //list Pageable total 封装三个数据,返回给页面
        return new PageImpl(list,pageable,total);
    }
}
